﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace AllInAI.Sharp.API.Dto {
    public class MessageDto {
        [JsonPropertyName("role")]
        public string Role { get; set; }
         [JsonIgnore]
        public string? Content { get; set; }

        [JsonIgnore] 
        public IList<MessageContent>? Contents { get; set; }

        /// <summary>
        ///     The contents of the message.
        /// </summary>
        [JsonPropertyName("content")]
        public object ContentCalculated {
            get {
                if (Content is not null && Contents is not null) {
                    throw new ValidationException(
                        "Content and Contents can not be assigned at the same time. One of them must be null."
                    );
                }

                if (Content is not null) {
                    return Content;
                }

                return Contents!;
            }
            set => Content = value?.ToString();
        }
        /// <summary>
        ///     The name of the author of this message. May contain a-z, A-Z, 0-9, and underscores, with a maximum length of 64 characters.
        /// </summary>
        [JsonPropertyName("name")]
        public string? Name { get; set; }

        /// <summary>
        ///     The name and arguments of a function that should be called, as generated by the model.
        /// </summary>
        [JsonPropertyName("function_call")]
        public FunctionCall? FunctionCall { get; set; }
    }
    public class FunctionCall {
        /// <summary>
        ///     Function name
        /// </summary>
        [JsonPropertyName("name")]
        public string? Name { get; set; }

        /// <summary>
        ///     Function arguments, returned as a JSON-encoded dictionary mapping
        ///     argument names to argument values.
        /// </summary>
        [JsonPropertyName("arguments")]
        public string? Arguments { get; set; }

        public Dictionary<string, object> ParseArguments() {
            var result = !string.IsNullOrWhiteSpace(Arguments) ? JsonSerializer.Deserialize<Dictionary<string, object>>(Arguments) : null;
            return result ?? new();
        }
    }
}
